from selenium import webdriver
from selenium.webdriver.chrome.service import Service
import time
from models import *
import uuid




def update_up_data(lock):
    service = Service('./edgedriver_win32/msedgedriver.exe')
    option = webdriver.EdgeOptions()
    option.add_argument('--headless')
    driver = webdriver.Edge(service=service,options=option)
    while(True):
        ups = Up.select()
        for up in ups:
            driver.get(up.url)
            time.sleep(2)
            fans = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div/div[1]/div[3]/div[2]/p[2]').text
            if up.fans!= fans:
                up.fans = fans
                if fans.find('万') != -1:
                    fans = int(float(fans.replace('万',''))*10000)
                else:
                    fans = int(fans)
                up.fans_num = fans
                lock.acquire()
                up.save()
                lock.release()
            driver.get(up.url+'/video')
            time.sleep(2)
            videos = []
            for i in range(1,6):
                url = driver.find_element(by='xpath', value='/html/body/div[2]/div[4]/div/div/div[2]/div[4]/div/div/ul[2]/li['+str(i)+']/a[2]').get_attribute('href')
                name = driver.find_element(by='xpath', value='/html/body/div[2]/div[4]/div/div/div[2]/div[4]/div/div/ul[2]/li['+str(i)+']/a[2]').text
                video = Video.get_or_none(Video.url == url and Video.upid == up.id and Video.name == name)
                if video is None:
                    image_url = driver.find_element(by='xpath', value='/html/body/div[2]/div[4]/div/div/div[2]/div[4]/div/div/ul[2]/li['+str(i)+']/a[1]/div[1]/picture/img').get_attribute('src')
                    image_url = image_url.split('@')[0]
                    video = {
                        'url': url,
                        'id': uuid.uuid4().hex,
                        'upid':up.id,
                        'image_url':image_url,
                    }
                    videos.append(video)
                else:
                    break
            for video in videos:
                driver.get(video["url"])
                time.sleep(2)
                video["name"] = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div[1]/div[1]/div[1]/div/h1').text
                video["date"] = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div[1]/div[1]/div[2]/div/div[3]/div').text
                video["time"] = time.mktime(time.strptime(video["date"], "%Y-%m-%d %H:%M:%S"))
            with lock:
                with database.atomic():
                    Video.insert_many(videos).execute()
        time.sleep(60)



def update_video_data_7d(lock):
    service = Service('./edgedriver_win32/msedgedriver.exe')
    option = webdriver.EdgeOptions()
    option.add_argument('--headless')
    driver = webdriver.Edge(service=service,options=option)
    while(True):
        videos = Video.select().where(Video.time >= time.time()-60*60*24*7)
        video_datas = [] 
        for video in videos:
            driver.get(video.url)
            time.sleep(2)
            views = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div[1]/div[1]/div[2]/div/div[1]/div').text
            if views.find('万') != -1:
                views = int(float(views.replace('万',''))*10000)
            else:
                views = int(views)
            likes = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div[1]/div[3]/div[1]/div[1]/div[1]/div/span').text
            if likes.find('万') != -1:
                likes = int(float(likes.replace('万',''))*10000)

            else:
                likes = int(likes)
            shares = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div[1]/div[3]/div[1]/div[1]/div[4]/div/span/div[2]/div/span').text
            if shares.find('万') != -1:
                shares = int(float(shares.replace('万',''))*10000)
            else:
                try:
                    shares = int(shares)
                except:
                    shares = 0
            collections = driver.find_element(by='xpath', value='/html/body/div[2]/div[2]/div[1]/div[3]/div[1]/div[1]/div[3]/div/span').text
            if collections.find('万') != -1:
                collections = int(float(collections.replace('万',''))*10000)
            else:
                collections = int(collections)
            video_data = {
                    "videoid" : video.id,
                    "likes": likes,
                    "date" :time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()),
                    'time' : time.time(),
                    "views": views,
                    "shares": shares,
                    "collections": collections,
                }
            video_datas.append(video_data)
        with lock:
            with database.atomic():
                VideoData.insert_many(video_datas).execute()
        time.sleep(60*10)

